package org.jeecg.modules.recruit.remittance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.modules.recruit.accounthistory.entity.MmAppAccountHistory;
import org.jeecg.modules.recruit.accounthistory.service.IMmAppAccountHistoryService;
import org.jeecg.modules.recruit.remittance.entity.MmAppRemittance;
import org.jeecg.modules.recruit.remittance.mapper.MmAppRemittanceMapper;
import org.jeecg.modules.recruit.remittance.service.IMmAppRemittanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 打款单
 * @Author: jeecg-boot
 * @Date: 2020-08-21
 * @Version: V1.0
 */
@Service
public class MmAppRemittanceServiceImpl extends ServiceImpl<MmAppRemittanceMapper, MmAppRemittance> implements IMmAppRemittanceService {

    @Autowired
    private IMmAppAccountHistoryService mmAppAccountHistoryService;

    @Override
    public boolean createRemittance(MmAppRemittance mmAppRemittance) {
        List<MmAppRemittance> list = lambdaQuery().eq(MmAppRemittance::getApplyId, mmAppRemittance.getApplyId()).list();
        if (CollectionUtils.isNotEmpty(list)) {
            throw new IllegalArgumentException("创建打款单失败！该申请已经存在打款单");
        }
        save(mmAppRemittance);
        return true;
    }

    @Override
    @Transactional
    public boolean updateRemittance(MmAppRemittance mmAppRemittance) {
        updateById(mmAppRemittance);
        if (mmAppRemittance.getStatus().equals(1)) {//打款成功增加mm_app_account_history记录
            MmAppAccountHistory history = new MmAppAccountHistory();
            history.setAccessType(2);
            history.setBusinessId(mmAppRemittance.getId());
            history.setMoneyAmount(mmAppRemittance.getRemitAmount());
            history.setOpTime(new Date());
            history.setUserId(mmAppRemittance.getUserId());
            mmAppAccountHistoryService.save(history);
        }
        return true;
    }
}
